package com.xiaoxiong.mami.http;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

/**
 * Created by Administrator on 2017/7/30.
 */
public class HttpUtil {


    public static String getRequestContent(HttpServletRequest request) {

        String requestContent = null;

        try {
            // 获取HTTP Body中的内容
            InputStream in = request.getInputStream();
            byte [] result = null;
            if (XHeader.contentGzip(request) && request.getMethod().equalsIgnoreCase("post")) {
                result = GZIPDecoder.decode(in);
            } else {
                int k = 0;
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                while ((k = in.read()) != -1) {
                    os.write(k);
                }
                result = os.toByteArray();
            }

            if (result != null) {
                requestContent = new String(result, "UTF-8");
            }
        } catch (Exception e) {
        }

        return requestContent;
    }


}
